﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;


/// <summary>
/// 2020年11月13日 09点05分
/// 巫云海
/// 新增全键盘控件
/// </summary>
namespace Bojay.UI
{

    [DefaultEvent("KeyDown")]
    public partial class UIKeyBorderAll : UserControl
    {
        /// <summary>
        /// The character type
        /// </summary>
        private KeyBorderCharType _charType = KeyBorderCharType.CHAR;

        /// <summary>
        /// Gets or sets the type of the character.
        /// </summary>
        /// <value>The type of the character.</value>
        [Description("默认显示样式"), Category("自定义")]
        public KeyBorderCharType CharType
        {
            get { return _charType; }
            set
            {
                _charType = value;
                if (value == KeyBorderCharType.CHAR)
                {
                    if (panel37.Text.ToLower() == "abc.")
                    {
                        CharOrNum();
                    }
                }
                else
                {
                    if (panel37.Text.ToLower() == "?123")
                    {
                        CharOrNum();
                    }
                }
            }
        }
        /// <summary>
        /// Occurs when [key click].
        /// </summary>
        [Description("按键点击事件"), Category("自定义")]
        public event EventHandler KeyClick;
        /// <summary>
        /// Occurs when [enter click].
        /// </summary>
        [Description("回车点击事件"), Category("自定义")]
        public event EventHandler EnterClick;
        /// <summary>
        /// Occurs when [backspace clike].
        /// </summary>
        [Description("删除点击事件"), Category("自定义")]
        public event EventHandler BackspaceClike;
        /// <summary>
        /// Occurs when [retract clike].
        /// </summary>
        [Description("收起点击事件"), Category("自定义")]
        public event EventHandler RetractClike;

        public UIKeyBorderAll()
        {
            InitializeComponent();
        }


        /// <summary>
        /// Handles the MouseDown event of the KeyDown control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="MouseEventArgs" /> instance containing the event data.</param>
        private void KeyDown_MouseDown(object sender, MouseEventArgs e)
        {
            UIButton btn = sender as UIButton;
            if (string.IsNullOrEmpty(btn.Text))
            {
                return;
            }
            if (btn.Text == "大写")
            {
                ToUper(true);
                btn.Text = "小写";
            }
            else if (btn.Text == "小写")
            {
                ToUper(false);
                btn.Text = "大写";
            }
            else if (btn.Text == "?123" || btn.Text == "abc.")
            {
                CharOrNum();
            }
            else if (btn.Text == "空格")
            {
                SendKeys.Send(" ");
            }
            else if (btn.Text == "删除")
            {
                SendKeys.Send("{BACKSPACE}");
                BackspaceClike?.Invoke(sender, e);
            }
            else if (btn.Text == "回车")
            {
                SendKeys.Send("{ENTER}");
                EnterClick?.Invoke(sender, e);
            }
            else if (btn.Text == "收起")
            {
                RetractClike?.Invoke(sender, e);
            }
            else
            {
                string Str = "{" + btn.Text + "}";
                SendKeys.Send(Str);
            }
            KeyClick?.Invoke(sender, e);
        }


        /// <summary>
        /// Converts to uper.
        /// </summary>
        /// <param name="bln">if set to <c>true</c> [BLN].</param>
        private void ToUper(bool bln)
        {
            foreach (Control item in this.tableLayoutPanel2.Controls)
            {
                if (item is UIButton)
                { 
                    if (item.Text == "abc.")
                        break;
                    if (bln)
                    {
                        item.Text = item.Text.ToUpper();
                    }
                    else
                    {
                        item.Text = item.Text.ToLower();
                    }

                }
            }
        }

        /// <summary>
        /// Characters the or number.
        /// </summary>
        private void CharOrNum()
        {
            foreach (Control item in this.tableLayoutPanel2.Controls)
            {
                if (item is UIButton)
                {
                    UIButton pnl = item as UIButton;
                    string strTag = pnl.Text;
                    pnl.Text = pnl.TagString;
                    pnl.TagString = strTag;
                }
            }
        }
    }

    /// <summary>
    /// Enum KeyBorderCharType
    /// </summary>
    public enum KeyBorderCharType
    {
        /// <summary>
        /// The character
        /// </summary>
        CHAR = 1,
        /// <summary>
        /// The number
        /// </summary>
        NUMBER = 2
    }
}
